import pyglet 
import os

class ResourceManager(object):
    "Generic resource manager altered from Pygame cookbook to work with Pyglet"
    
    def __init__(self, rootdir):
        "Clear the map of names and the cache."
        self.__dict__.update(dict(
            names = {},
            cache = {},
            rootdir = rootdir
        ))
        
        
    def __setattr__(self, name, filename):
        " Create a managed resource."
        
        # Store the filename of the resource under its name
        self.names[name] = os.path.join(self.rootdir, filename)

    
    def __getattr__(self, name):
        "Get the actual resource."
        
        # If the resource is already in the cache, get it.
        try:
            resource = self.cache[name]
            
        # Otherwise we need to load the resource. 
        except KeyError:
            
            # Call the loader with the filename ...
            try:
                resource = self.loader(self.names[name])
                
                # ... and store it in the cache.
                self.cache[name] = resource
                
            # If the key doesn't exist
            except KeyError:
                
                # Raise the correct exception
                raise AttributeError, name
                
        # Return the resource
        return resource

    
    def loader(self, filename):
        "Generic resource loader."
        
        # Use the resource value as a filename and open the file.
        file=open(filename, 'rb')
        
        # Read the contents of the file as the resource.
        return file.read()



class ImageManager(ResourceManager):
    "Resource manager for images"
    def __init__(self, rootdir):
        "Init the superclass"
        ResourceManager.__init__(self, rootdir)
    
    def loader(self, filename):
        "Loads image"
        image = pyglet.image.load(filename)
        image.anchor_x = image.width/2
        image.anchor_y = image.height/2
        return image
        
class SoundManager(ResourceManager):
    def __init__(self, rootdir):
        "Init the superclass"
        ResourceManager.__init__(self, rootdir)
        
    def loader(self, filename):
        "Loads sound"
        return pyglet.media.load(filename,streaming=False)


global Images
Images = ImageManager("res")

Images.menu_overlay = "menu_overlay.png"

Images.analog_stick_base = "analog_stick_base.png"
Images.analog_stick = "analog_stick_top.png"

Images.photon_bg = "photon_bg.jpg"
Images.colorful_bg = "colorful_bg.png"

Images.ground_box = "ground_box.png"
Images.bumper = "bumper.png"

Images.box = "box.png"
Images.box_blur = "box_blur.png"

Images.semiautorifle = "semiautorifle.png"
Images.semiautoriflebullet = "bullet.png"

Images.kunai = "kunai.png"

Images.crosshair = "crosshair.png"

global Sound
Sound = SoundManager("res")

